elementobject.eventproperty = eventhandler;
<body> <button id='button'>Click Me!</button> <!-- There is no invocation of event handler here. --> <h id='demo'></h> <script> function clickme() { document.getElementById('demo').innerHTML = 'Hmmm'; } document.getElementById('button').onclick = clickme; </script> </body>
<head> <style> ... </style> <script> ... </script> </head> <body> ... </body>
<head> <script> // The W3C model function clickme() { document.getElementById('demo').innerHTML = 'Hmmm'; } document.getElementById('button').addEventListener('click', clickme); // NOT the invocation of an event handler /* Or the traditional model function clickme() { document.getElementById('demo').innerHTML = 'Hmmm'; } document.getElementById('button').onclick = clickme; // NOT the invocation of an event handler */ /* Or document.getElementById('button').onclick = function() { // Anonymouse function - a function that has no name document.getElementById('demo').innerHTML = 'Hmmm'; } */ </script> </head> <body> <button id='button'>Click Me!</button> <!-- There is no invocation of event handler here. --> <p id='demo'></p> </body>
document.getElementById('button').addEventListener(...);
should be executed after <body> is completed loaded.<head> <script> function clickme() { document.getElementById('demo').innerHTML = 'Hmmm'; } function start() { document.getElementById('button').addEventListener('click', clickme); // NOT the invocation of an event handler } window.addEventListener('load', start); // This event is triggered when the <body> is completely loaded. // The second argument is NOT the invocation of an event handler. /* OR window.addEventListener('???', ???() { document.getElementById('button').addEventListener('click', clickme); }); */ </script> </head> <body> <button id='button'>Click Me!</button> <!-- There is no invocation of event handler here. --> <p id='demo'></p> </body>
Username: <input type='text' value='Wow!'><br> <label for='pswd'>Password:</label> <input id='pswd' type='password' style='width:200px' autofocus><br> Car: <input type='radio' name='car' value='VW'>VW <input is='ford' type='radio' name='car' value='Ford'><label for='ford'>Ford</label> <input type='radio' name='car' value='Hyundai' checked>Hyundai<br> Course: <input type='checkbox' name='course' value='2680' checked>2680 <input type='checkbox' name='course' value='3540' checked>3540 <input type='checkbox' name='course' value='4620'>4620 <input type='checkbox' name='course' value='4680'>4680 <input type='checkbox' name='course' value='4910'>4910<br> <label for='sz'>Size:</label> <input id='sz' type='number' min=2 max=10 value='3'><br> <input type='submit' value='Submit'>
multiple
document.getElementById('control').addEventListener('click', eh); function eh(event) { // event is an Event object. The browser invokes this function with the Event object. event.target.innerHTML = 'Hmmm'; // target: the element object on which this event is triggered. }
eventObj.key
eventObj.clientX, event.clientY
eventObj.target.id
clearTimeout(timerid)
clearInterval(timerid)
<script> ??? count; function start() { document.???('start_button').??? (???, function() { count = ???; window.setInterval(function() { count???; ???.getElementById('seconds').??? = count; },???); // Every 1 second }; } ???.???(???, start); // When <body> is completely loaded. </script> <body> <button id='start_button'>Start the game</button><br> <p>Elapsed time: <span id='seconds></span></p> </body>
write(), getElementById(), addEventListener()
elementobj.attributename, elementobj.getAttribute(name), elementobj.setAttribute(name, value)
elementobj.style.csspropertyname, elementobj.style.getProperty(name), elementobj.style.setProperty(name, value)
window.innerHeight, window.innerWidth
document, alert(), setInterval(), setTimeout()
load, resize
createElement(), createTextNode(), appendChild()
newlycreatedelementobj.id = 'newid'; newlycreatedelementobj.setAttribute('id', 'newid');
elementobj.childNodes